#!/bin/bash
############################
##docker 安装方法
##./docker.sh install
############################
dockerInstall() {
  echo "开始安装docker..."
  echo "卸载旧版本ducker"
  sudo yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine
  echo "设置仓库"
  echo "安装所需的软件包。yum-utils 提供了 yum-config-manager ，并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2"
  sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
  echo "设置阿里云"
  sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  echo "安装docker社区免费版"
  sudo yum install -y docker-ce docker-ce-cli containerd.io
  echo "启动docker"
  sudo systemctl start docker
  echo "docker测试"
  sudo docker run hello-world
}
############################
##docker 卸载方法
##./docker.sh remove
############################
dockerRemove() {
  echo "开始卸载docker..."
  yum -y remove docker-ce
  echo "删除镜像、容器、配置文件等内容"
  rm -rf /var/lib/docker
  echo "docker 卸载成功"
}
############################
##docker 查询镜像版本
##./docker.sh list $name
############################
list() {
  name=$1
  curl https://registry.hub.docker.com/v1/repositories/$name/tags |
    tr -d '[\[\]" ]' | tr '}' '\n' |
    awk -F: -v image=$name '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}' |
    tail -20
}
############################
##docker nginx run
##./docker.sh nginx $pwd
############################
nginx() {
  home=$1
  container=nginx
  container_image_pull $container
  if [ ! -d $home ]; then
    echo "$home文件夹不存在，创建$home..."
    mkdir $home
  fi
  if [ ! -d $home/conf ]; then
    echo "$home/conf文件夹不存在，创建$home/conf"
    mkdir $home/conf
  fi
  if [ ! -d $home/www ]; then
    echo "$home/www文件夹不存在,创建$home/www"
    mkdir $home/www
  fi
  if [ ! -d $home/logs ]; then
    echo "$home/logs文件夹不存在，创建$home/logs..."
    mkdir $home/logs
  fi
  if [ ! -f $home/conf/nginx.conf ]; then
    echo "$home/conf/nginx.conf文件不存在，创建文件$home/conf/nginx.conf文件..."
    cat >$home/conf/nginx.conf <<EOF
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
EOF
  fi


  echo "执行docker命令\
	docker run \
	--name nginx\
	-p 80:80\
	-v $home/www:/usr/share/nginx/html\
	-v $home/conf/nginx.conf:/etc/nginx/nginx.conf\
	-v $home/logs:/var/log/nginx\
	-d  $container"
  #判断容器是否已经启动
  container_ps_remove $container
  docker run \
    --name nginx \
    -p 80:80 \
    -v $home/www:/usr/share/nginx/html \
    -v $home/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v $home/logs:/var/log/nginx \
    -d $container
  exist_2=$(docker inspect --format '{{.State.Running}}' ${container})
  if [ "true" == $exist_2 ]; then
    echo "nginx 启动成功，容器名称为:$container"
  else
    echo "nginx 启动失败"
  fi
}

############################
##docker mysql run
##./docker.sh mysql
############################
mysql() {
  home=$1
  container=mysql
  container_image_pull $container
  if [ ! -d $home ]; then
    mkdir $home
  fi
  if [ ! -d $home/mysqld ]; then
    mkdir $home/mysqld
  fi
  if [ ! -d $home/db ]; then
    mkdir $home/db
  fi
  if [ ! -d $home/conf ]; then
    mkdir $home/conf
  fi
  if [ ! -d $home/files ]; then
    mkdir $home/files
  fi
  #权限组
  chown -R polkitd:input $home

  #判断容器是否已经启动
  container_ps_remove $container

  docker run \
    --name=mysql -it \
    -p 3366:3306 \
    -v $home/mysqld:/var/run/mysqld \
    -v $home/db:/var/lib/mysql \
    -v $home/conf:/etc/mysql/conf.d \
    -v $home/files:/var/lib/mysql-files \
    -e MYSQL_ROOT_PASSWORD=wwqwwc*140316 \
    --privileged=true \
    -d mysql
	
	
######开启远程连接权限
###### grant all privileges on *.* to 'root'@'%' with grant option;
###### ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'wwqwwc*140316';
###### GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

  echo "mysql docker 容器安装成功"
  echo "mysql 端口: 3366"
  echo "mysql 用户: root"
  echo "mysql 密码: wwqwwc*140316"
}

############################
##docker redis run
##./docker.sh redis
############################
redis() {
  home=$1
  container=redis
  container_image_pull $container
  if [ ! -d $home ]; then
    mkdir $home
  fi
  if [ ! -d $home/data ]; then
    mkdir $home/data
  fi
  if [ ! -d $home/redis.conf ]; then
    mkdir $home/redis.conf
  fi
  #删除容器进程
  container_ps_remove $container
  docker run \
    --name=$container \
    -p 6699:6379 \
    -v /root/reids/redis.conf:/etc/redis/redis.conf \
    -v /root/reids/data:/data \
    -d $container \
    redis-server /etc/redis/redis.conf \
    --appendonly yes \
    --requirepass wwqwwc*140316

  echo "redis docker 容器安装成功"
  echo "redis 端口: 6699"
  echo "reids 密码: wwqwwc*140316"
}

############################
##docker tomcat run
##./docker.sh tomcat
############################
tomcat() {
  home=$1
  container=tomcat
  container_image_pull $container
  if [ ! -d $home ]; then
    mkdir $home
  fi
  if [ ! -d $home/logs ]; then
    mkdir $home/logs
  fi
  if [ ! -d $home/conf ]; then
    mkdir $home/conf
  fi
  if [ ! -d $home/webapps ]; then
    mkdir $home/webapps
  fi
  docker run --name $container \
    -p 8080:8080\
    -v $home/logs:/web/domain/server/tomcat/logs\
    -v $home/conf:/web/domain/server/tomcat/conf\
    -v $home/webapps:/web/domain/server/tomcat/webapps \
    -d \
    $container
}
####################
##下载容器镜像
####################
container_image_pull(){
  container=$1
  laster=$2
  exist=$(docker images | grep $container | awk '{print $1}')
  echo "监测镜像是否安装 container=$container exist=$exist"
  if [ !$container == exist ]; then
    echo "监测镜像 container=$container 不存在，开始docker pull"
    docker pull $container
  fi
}

######################
##关闭容器进程
######################
container_ps_remove(){
  container=$1
  #判断容器是否已经启动
  exist=$(docker inspect --format '{{.State.Running}}' ${container})
  #如果启动，则停止，删除容器
  if [ "true" == $exist ]; then
    echo "docker 停止容器进程 $container"
    docker stop $container
    echo "docker 删除容器进程 $container"
    docker rm $container
  else
    echo "docker 删除容器进程 $container"
    docker rm $container
  fi
}

cmd=$1
nginx_home=/root/nginx
redis_home=/root/redis
mysql_home=/root/mysql
nacos_home=/root/nacos
elasticsearch_home=/root/elasticsearch
tomcat_home=/root/tomcat
echo "接受指令:$cmd"

if [ "install" == $cmd ]; then
  echo "执行安装docker函数 dockerInstall..."
  dockerInstall
elif [ "remove" == $cmd ]; then
  echo "执行卸载docker函数 dockerRemove..."
  dockerRemove
elif [ "list" == $cmd ]; then
  echo "查询docker前20版本..."
  list $2
elif [ "nginx" == $cmd ]; then
  echo "开始安装nginx  根路径为$cmd"
  nginx $nginx_home
elif [ "tomcat" == $cmd ]; then
  echo "开始安装tomcat  根路径为$tomcat_home"
  tomcat $tomcat_home
elif [ "redis" == $cmd ]; then
  echo "开始安装redis  根路径为$redis_home"
  redis $redis_home
elif [ "mysql" == $cmd ]; then
  echo "开始安装mysql  根路径为$mysql_home"
  mysql $mysql_home
else
  echo "无可执行命令"
fi
